package com.angel.controller;

import com.angel.bean.CmsInfo;
import com.angel.bean.Employee;
import com.angel.service.CmsInfoService;
import com.angel.service.EmployeeService;
import com.angel.utils.NewsBin;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *预留位置(现在用于新闻显示)
 */
@Controller
@RequestMapping("/res")
public class UserNewsController {

    @Autowired
    private CmsInfoService cmsInfoService;

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/reserved")
    public String reserved(@RequestParam(value="posion", defaultValue="1")String posion,Model model){
        int pageNo=1;
        try {
            pageNo = Integer.valueOf(posion);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 5);//设置分页
        try {
            List<CmsInfo> cmsInfoList=cmsInfoService.findAllStart(null);
            //把信息存入NewsBinLsit里面,然后转放入cmsInfoList里面
            List<NewsBin> newsBinList=new ArrayList<>();
            for (int i=0 ; i<cmsInfoList.size() ; i++){
                NewsBin newsBin=new NewsBin();
                //查找编辑者
                try {
                    Employee employee=new Employee();
                    employee.setEmId(cmsInfoList.get(i).getEditor());
                    Employee result=employeeService.findOne(employee);
                    newsBin.setEditor(result.getEmName());//编辑者
                }catch (Exception e){
                     System.out.println("编辑者异常");
                }
                //提取图片
                String conten=cmsInfoList.get(i).getContent();
                //
                Set<String> pics = new HashSet<>();
                String img = "";
                Pattern p_image;
                Matcher m_image;
                //     String regEx_img = "<img.*src=(.*?)[^>]*?>"; //图片链接地址
                String regEx_img = "<img.*src\\s*=\\s*(.*?)[^>]*?>";
                p_image = Pattern.compile
                        (regEx_img, Pattern.CASE_INSENSITIVE);
                m_image = p_image.matcher(conten);
                while (m_image.find()) {
                    // 得到<img />数据
                    img = m_image.group();
                    // 匹配<img>中的src数据
                    Matcher m = Pattern.compile("src\\s*=\\s*\"?(.*?)(\"|>|\\s+)").matcher(img);
                    while (m.find()) {
                        pics.add(m.group(1));
                        break;
                    }
                }
                String url_path=pics.toString();
                url_path=url_path.substring(1,url_path.length()-1);
                System.out.println(url_path+"第：--"+i);
                //提取结束
                newsBin.setUrl_path(url_path);
                newsBinList.add(newsBin);
            }
            for (int i=0 ; i< cmsInfoList.size() ; i++){
                cmsInfoList.get(i).setReserve1(newsBinList.get(i).getEditor());//编辑者
                cmsInfoList.get(i).setReserve2(newsBinList.get(i).getUrl_path());//图片luj
            }
            PageInfo<CmsInfo> page=new PageInfo<>(cmsInfoList);
            model.addAttribute("emp",true);
            model.addAttribute("pageInfo",page);
        }catch (Exception e){
            model.addAttribute("emp",false);
        }
        return "user_news";
    }

    @RequestMapping("/newsInfo")
    public String newsInfor(Integer id,Model model){
        CmsInfo cmsInfo=new CmsInfo();
        cmsInfo.setId(id);
        try {
            CmsInfo result=cmsInfoService.findOne(cmsInfo);
            model.addAttribute("emp",true);
            model.addAttribute("titel",result.getTitle());
            model.addAttribute("connt",result.getContent());
        }catch (Exception e){
            model.addAttribute("emp",false);
        }
        return "user_news_infor";
    }
}
